package com.supermap.dataservice.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * @Description: 计算工具类
 * @author zhoujian
 * @date 2018-3-2
 * @version V1.0
 */
public class Arith
{
	private static final int DEF_DIV_SCALE_ZERO = 0;

	/**
	 * 
	 * @Description:取绝对值
	 * @return int
	 * @throws
	 */
	public static int abs(int arg)
	{
		BigDecimal a = new BigDecimal(arg);
		return a.abs().intValue();
	}

	public static short abs(short arg)
	{
		BigDecimal a = new BigDecimal(arg);
		return a.abs().shortValue();
	}

	public static long abs(long arg)
	{
		BigDecimal a = new BigDecimal(arg);
		return a.abs().longValue();
	}

	public static float abs(float arg)
	{
		BigDecimal a = new BigDecimal(arg);
		return a.abs().floatValue();
	}

	public static double abs(double arg)
	{
		BigDecimal a = new BigDecimal(Double.toString(arg));
		return a.abs().doubleValue();
	}
	/**
	 * 
	 * @Description: 比较
	 * @return boolean
	 * @throws
	 */
	public static boolean equals(float arg1, float arg2)
	{
		boolean isEqual = false;
		BigDecimal a = new BigDecimal(Float.toString(arg1));
		BigDecimal b = new BigDecimal(Float.toString(arg2));
		if (a.compareTo(b) == 0) isEqual = true;
		return isEqual;
	}

	public static boolean equals(double arg1, double arg2)
	{
		boolean isEqual = false;
		BigDecimal a = new BigDecimal(Double.toString(arg1));
		BigDecimal b = new BigDecimal(Double.toString(arg2));
		if (a.compareTo(b) == 0) isEqual = true;
		return isEqual;
	}

	/**
	 * 
	 * @Description: 加法
	 * @return float
	 * @throws
	 */
	public static float add(float arg1, float arg2)
	{
		BigDecimal a = new BigDecimal(Float.toString(arg1));
		BigDecimal b = new BigDecimal(Float.toString(arg2));
		return a.add(b).floatValue();
	}

	public static double add(double arg1, double arg2)
	{
		BigDecimal a = new BigDecimal(Double.toString(arg1));
		BigDecimal b = new BigDecimal(Double.toString(arg2));
		return a.add(b).doubleValue();
	}
	/**
	 * 
	 * @Description: 减法
	 * @return float
	 * @throws
	 */
	public static float sub(float arg1, float arg2)
	{
		BigDecimal a = new BigDecimal(Float.toString(arg1));
		BigDecimal b = new BigDecimal(Float.toString(arg2));
		return a.subtract(b).floatValue();
	}

	public static double sub(double arg1, double arg2)
	{
		BigDecimal a = new BigDecimal(Double.toString(arg1));
		BigDecimal b = new BigDecimal(Double.toString(arg2));
		return a.subtract(b).doubleValue();
	}
	/**
	 * 
	 * @Description: 乘法
	 * @return float
	 * @throws
	 */
	public static float mul(float arg1, float arg2)
	{
		BigDecimal a = new BigDecimal(Float.toString(arg1));
		BigDecimal b = new BigDecimal(Float.toString(arg2));
		return a.multiply(b).floatValue();
	}

	public static double mul(double arg1, double arg2)
	{
		BigDecimal a = new BigDecimal(Double.toString(arg1));
		BigDecimal b = new BigDecimal(Double.toString(arg2));
		return a.multiply(b).doubleValue();
	}
	/**
	 * 
	 * @Description: 除法
	 * @return float
	 * @throws
	 */
	public static float div(float arg1, float arg2)
	{
		return div(arg1, arg2, 0);
	}

	public static double div(double arg1, double arg2)
	{
		return div(arg1, arg2, 0);
	}

	public static double div(double arg1, double arg2, int scale)
	{
		if (scale < 0)
		{
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal a = new BigDecimal(Double.toString(arg1));
		BigDecimal b = new BigDecimal(Double.toString(arg2));
		return a.divide(b, scale, 4).doubleValue();
	}

	public static float div(float arg1, float arg2, int scale)
	{
		if (scale < 0)
		{
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal a = new BigDecimal(Float.toString(arg1));
		BigDecimal b = new BigDecimal(Float.toString(arg2));
		return a.divide(b, scale, 4).floatValue();
	}
	/**
	 * 
	 * @Description: 四舍五入
	 * @return double
	 * @throws
	 */
	public static double round(double arg, int scale)
	{
		if (scale < 0)
		{
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(arg);
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, 4).doubleValue();
	}

	public static float round(float arg, int scale)
	{
		if (scale < 0)
		{
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(arg);
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, 4).floatValue();
	}
	/**
	 * 
	 * @Description: float 转换 String
	 * @return String
	 * @throws
	 */
	public static String floatToString(float arg)
	{
		String result = null;
		BigDecimal a = new BigDecimal(arg);
		DecimalFormat decimalFormat = new DecimalFormat("0.00");
		result = decimalFormat.format(a);
		return result;
	}
	
	public static String floatToString(float arg, int scale)
	{
		String result = null;
		BigDecimal a = new BigDecimal(arg);
		DecimalFormat decimalFormat = null;
		if (scale == 0)
		{
			decimalFormat = new DecimalFormat("0");
		}
		else if (scale == 1)
		{
			decimalFormat = new DecimalFormat("0.0");
		}
		else if (scale == 2)
		{
			decimalFormat = new DecimalFormat("0.00");
		}
		else if (scale == 3)
		{
			decimalFormat = new DecimalFormat("0.000");
		}
		else if (scale == 4)
		{
			decimalFormat = new DecimalFormat("0.0000");
		}
		else
		{
			decimalFormat = new DecimalFormat("0.00000");
		}
		result = decimalFormat.format(a);
		return result;
	}

	/**
	 * 
	 * @Description: float 转 百分比
	 * @return String
	 * @throws
	 */
	public static String floatToPercentString(float arg, int scale)
	{
		BigDecimal b1 = new BigDecimal(arg);
		DecimalFormat decimalFormat = null;
		if (scale == 0) decimalFormat = new DecimalFormat("0%");
		else if (scale == 1) decimalFormat = new DecimalFormat("0.0%");
		else if (scale == 2) decimalFormat = new DecimalFormat("0.00%");
		String result = decimalFormat.format(b1);
		return result;
	}
}
